from abc import ABCMeta, abstractmethod
import time


class Observer(metaclass=ABCMeta):

    @abstractmethod
    def update(self, observable, obj):
        pass


class WashMode(Observer):

    def update(self, observable, obj):
        if 40 <= observable.temperature <= 50:
            print('温度正好，可以洗澡了')


class DrinkMode(Observer):

    def update(self, observable, obj):
        if 99 <= observable.temperature:
            print('水已经开了，可以引用了')


class Observable(object):
    # 被观察者 要实现 观察者的增删查

    def __init__(self):
        self.__observers = list()

    @property
    def observers(self):
        return self.__observers

    def add(self, *args, **kwargs):
        for arg in args:
            if isinstance(arg, Observer):
                self.__observers.append(arg)

    def remove(self, observer):
        if isinstance(observer, Observer):
            if observer in self.__observers:
                self.__observers.remove(observer)

    def clear(self):
        self.__observers.clear()


class WaterHeater(Observable):

    def __init__(self):
        super(WaterHeater, self).__init__()
        self.add(WashMode(), DrinkMode())
        self.__temperature = 0

    @property
    def temperature(self):
        return self.__temperature

    @temperature.setter
    def temperature(self, tempe):
        if isinstance(tempe, (int, float)):
            pass
        elif isinstance(tempe, str) and tempe.isalnum():
            tempe = float(tempe)
        else:
            print('输入温度数据不合法')
            return

        if -50 < tempe <= 110:
            self.__temperature = tempe
            print('当前水温是:%s' % self.__temperature)
            self.notify()
        else:
            print('输入温度范围不合理')
            return

    def notify(self):
        for observer in self.observers:
            observer.update(self, '')


if __name__ == '__main__':
    water_heater = WaterHeater()
    while True:
        time.sleep(1)
        water_heater.temperature += 5


